﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.DMP.DB;
using FastCloud.DMP.Entity;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.DMP
{
    [Route("frame/dmp/project")]
    public class ProjectController : BaseController
    {
        [AutoWired]
        private DB_Project DB_Project;
        [AutoWired]
        private DB_Table DB_Table;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/DMP/Project/Project_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/DMP/Project/Project_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/DMP/Project/Project_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ProjectName = ValueConvert.ConvertToString(GetFormParam("ProjectName"));
            string ParentProjectGuid = ValueConvert.ConvertToString(GetFormParam("ParentProjectGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(ProjectName))
            {
                sql += " and ProjectName like @ProjectName";
                ParamList.Add(new ParamEntity() { ParamName = "ProjectName", ParamValue = "%" + ProjectName + "%" });
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(ParentProjectGuid))
                {
                    sql += " and ParentProjectGuid=@ParentProjectGuid";
                    ParamList.Add(new ParamEntity() { ParamName = "ParentProjectGuid", ParamValue= ParentProjectGuid });
                }
            }
            return Result.OK(DB_Project.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Project Entity)
        {
            DB_Project.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Project Entity)
        {
            DB_Project.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ProjectGuid = ValueConvert.ConvertToString(GetFormParam("ProjectGuid"));
            return Result.OK(DB_Project.SelectOne(ProjectGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var item in KeyIDList)
            {
                Frame_Project Project = DB_Project.SelectOne(item);
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ParentProjectGuid", ParamValue = item } };
                int SubCount = DB_Project.SelectCount("ParentProjectGuid=@ParentProjectGuid", ParamList);
                if (SubCount > 0)
                {
                    return Result.Error(string.Format("项目{0}包含子项目，无法删除", Project.ProjectName));
                }
                int TableCount = DB_Table.SelectCount("ProjectGuid=@ProjectGuid", new List<ParamEntity>() { new ParamEntity() { ParamName = "ProjectGuid", ParamValue = item } });
                if (TableCount > 0)
                {
                    return Result.Error(string.Format("项目{0}包含表结构，无法删除", Project.ProjectName));
                }
            }
            DB_Project.DeleteBatch(KeyIDList);
            return Result.OK();
        }

        [Route("tree"), HttpPost]
        public IActionResult Tree()
        {
            List<Frame_Project> GroupList = DB_Project.SelectAllList();
            GroupList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            Frame_Project Root = new Frame_Project();
            Root.ProjectGuid = "Top";
            Root.ProjectName = "全部项目";
            Root.ParentProjectGuid = "";
            GroupList.Add(Root);
            foreach (Frame_Project item in GroupList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.ProjectGuid;
                node.Name = item.ProjectName;
                node.PID = item.ParentProjectGuid;
                if (item.ProjectGuid == Root.ProjectGuid)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }
    }
}
